#include "stdafx.h"
#include "CodeByUTF8.h"


CodeByUTF8 * CodeByUTF8::obj = new CodeByUTF8();

CodeByUTF8::CodeByUTF8():args("")
{

}

void CodeByUTF8::setArgs(const std::string & args)
{
	this->args = args;
}


CodeByUTF8 * CodeByUTF8::getInstance(const std::string & args)
{
	obj->setArgs(args);

	return obj;
}

CodeByUTF8::~CodeByUTF8()
{
	delete & this->obj;
}

void CodeByUTF8::destory()
{
	delete this->obj;

	delete &(this->args);
}

std::string CodeByUTF8::decodeURL()
{
	const char * temp = (this->args.c_str());

	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, temp, -1, NULL, 0);

	wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1，不然会出现尾巴 
	ZeroMemory(pwBuf, nwLen * 2 + 2);

	::MultiByteToWideChar(CP_ACP, 0, temp, this->args.length(), pwBuf, nwLen);

	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

	char * pBuf = new char[nLen + 1];
	ZeroMemory(pBuf, nLen + 1);

	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string retStr(pBuf);

	delete[]pwBuf;
	delete[]pBuf;

	pwBuf = NULL;
	pBuf = NULL;

	return retStr;
}